package com.usian.admin.service.impl;

import com.usian.admin.feign.ArticleFeign;
import com.usian.admin.feign.WemediaFeign;
import com.usian.admin.service.AdChannelService;
import com.usian.admin.service.NewsAuthService;
import com.usian.model.admin.dtos.NewsAuthDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.article.pojos.ApArticle;
import com.usian.model.article.pojos.ApArticleConfig;
import com.usian.model.article.pojos.ApArticleContent;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmNews;
import com.usian.model.media.pojos.WmUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class NewsAuthServiceImpl implements NewsAuthService {

    @Autowired
    private WemediaFeign wemediaFeign;

    @Value("${fastdfs.url}")
    private String fastServiceUrl;

    @Autowired
    private ArticleFeign articleFeign;

    @Autowired
    private AdChannelService adChannelServiceI;

    @Override
    public ResponseResult findList(NewsAuthDto newsAuthDto) {
        ResponseResult responseResult = wemediaFeign.findList(newsAuthDto);
        responseResult.setHost(fastServiceUrl);
        return responseResult;
    }

    @Override
    public ResponseResult findOne(Integer id) {
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        ResponseResult responseResult = wemediaFeign.findOne(id);
        responseResult.setHost(fastServiceUrl);
        return responseResult;
    }

    @Override
    public ResponseResult newsAuth(NewsAuthDto newsAuthDto, int type) {
        // 变更wm_news的状态和驳回原因
        if (newsAuthDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        WmNews wmNews = new WmNews();
        wmNews.setId(newsAuthDto.getId());

        if (type == 1) {
            // 审核通过
            wmNews = wemediaFeign.findNewsById(newsAuthDto.getId());
            saveApArticle(wmNews);
        } else {
            // 审核失败
            updateWmNews(wmNews, WmNews.Status.FAIL.getCode(), newsAuthDto.getMsg());
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    private void saveApArticle(WmNews wmNews){
        // 1.保存文章相关信息
        // 1.1.同步 ap_article
        ApArticle apArticle = saveArticle(wmNews);
        // 1.2.同步 ap_article_config
        saveApArticleConfig(apArticle.getId());
        // 1.3.同步 ap_article_content
        saveApArticleContent(apArticle.getId(), wmNews.getContent());
        // 2.创建ES索引库  TODO
        // 3.变更WmNews的状态为9(已发布)
        wmNews.setArticleId(apArticle.getId());
        updateWmNews(wmNews, WmNews.Status.PUBLISHED.getCode(), "文章人工审核通过，已发布");
    }


    /**
     * 功能描述：同步ap_article_content
     * @param: articleId:文章id  content:文章内容
     * @return：
     * @auther：
     */
    private void saveApArticleContent(Long articleId, String content) {
        ApArticleContent apArticleContent = new ApArticleContent();
        apArticleContent.setArticleId(articleId);
        apArticleContent.setContent(content);
        articleFeign.saveArticleContent(apArticleContent);
    }

    /**
     * 功能描述：同步 ap_article_config
     * @param: 文章id
     * @return：
     * @auther：
     */
    private void saveApArticleConfig(Long articleId) {
        ApArticleConfig apArticleConfig = new ApArticleConfig();
        apArticleConfig.setArticleId(articleId);
        apArticleConfig.setIsComment(true);
        apArticleConfig.setIsDelete(false);
        apArticleConfig.setIsDown(false);
        apArticleConfig.setIsForward(true);
        articleFeign.saveArticleConfig(apArticleConfig);
    }




    /**
     * 功能描述：保存ap_article
     * @param:
     * @return：
     * @auther：
     */
    private ApArticle saveArticle(WmNews wmNews) {
        ApArticle apArticle = new ApArticle();
        // 赋值
        apArticle.setTitle(wmNews.getTitle());
        WmUser wmUser = wemediaFeign.findUserById(wmNews.getUserId());
        ApAuthor apAuthor = articleFeign.findByName(wmUser.getName());
        if (apAuthor != null) {
            apArticle.setAuthorId(apAuthor.getId().longValue());
            apArticle.setAuthorName(apAuthor.getName());
        }
        AdChannel adChannel = adChannelServiceI.getById(wmNews.getChannelId());
        if (adChannel != null) {
            apArticle.setChannelId(adChannel.getId());
            apArticle.setChannelName(adChannel.getName());
        }
        apArticle.setLayout(wmNews.getType());
        apArticle.setImages(wmNews.getImages());
        apArticle.setLabels(wmNews.getLabels());
        apArticle.setCreatedTime(new Date());
        apArticle.setPublishTime(new Date());
        apArticle = articleFeign.saveArticle(apArticle);
        return apArticle;
    }

    private void updateWmNews(WmNews wmNews, Short status, String reason){
        wmNews.setStatus(status);
        wmNews.setReason(reason);
        wemediaFeign.updateNewsStatus(wmNews);
    }
}
